﻿using ChamCongMain.Form;
using BLL;
using DevExpress.XtraEditors;
using System.Windows.Forms;
using System;
using System.Diagnostics;
namespace ChamCongMain.UserControl
{
    public partial class uclPhanCong : DevExpress.XtraEditors.XtraUserControl
    {
        
        public uclPhanCong()
        {
            InitializeComponent();
            PhanCongBLL pcbll = new PhanCongBLL();
            grcPhanCong.DataSource = pcbll.GetAllPhanCong();
        }
        private void RefeshG(object formName)
        {
            grcPhanCong.DataSource = formName;
        }
        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmPhanCong frm = new frmPhanCong(ChucNang.Them,null);
            frm.LoadData = new Delegates.RefeshGrid(RefeshG);
            frm.ShowDialog();
        }

        private void btnXuatExcel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
            if (gvPhanCong.RowCount == 0)
            {
                XtraMessageBox.Show("Không có dữ liệu để xuất file !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Cursor = System.Windows.Forms.Cursors.Default;
                return;
            }
            DevExpress.XtraGrid.Views.Grid.GridView grid = grcPhanCong.MainView as DevExpress.XtraGrid.Views.Grid.GridView;
            if (grid != null)
            {
                SaveFileDialog saveExcel = new SaveFileDialog();
                saveExcel.Filter = "Excel Workbook|*.xlsx|Excel Macro-Enabled Workbook|*.xlsm|Excel 97-2003 Workbook|*.xls";// định giạng file lưu
                saveExcel.Title = "Save an Excel File";
                saveExcel.InitialDirectory = @"C:\";
                if (saveExcel.ShowDialog() == DialogResult.OK)
                {
                    this.Cursor = System.Windows.Forms.Cursors.Default;
                    grid.ExportToXlsx(saveExcel.FileName);
                    XtraMessageBox.Show("Export File thành công!!!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (XtraMessageBox.Show("Bạn có muốn xem file ?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        Process exportXlsx = new Process();
                        exportXlsx.StartInfo.FileName = "EXCEL.exe";
                        exportXlsx.StartInfo.Arguments = saveExcel.FileName;
                        exportXlsx.Start();
                    }
                }
            }
            this.Cursor = System.Windows.Forms.Cursors.Default;
        }

        private void btnSua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            PhanCong cn = (PhanCong)gvPhanCong.GetRow(gvPhanCong.FocusedRowHandle);
            if (cn == null)
            {
                XtraMessageBox.Show("Không có dữ liệu để sửa !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                frmPhanCong frm = new frmPhanCong(ChucNang.Sua, cn);
                frm.LoadData = new Delegates.RefeshGrid(RefeshG);
                frm.ShowDialog();
            }
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            PhanCong cn = (PhanCong)gvPhanCong.GetRow(gvPhanCong.FocusedRowHandle);
            if (cn == null)
            {
                this.Cursor = Cursors.Default;
                return;
            }
            else
            {
                PhanCongBLL cnbll = new PhanCongBLL();
                try
                {
                    cnbll.DeletePhanCong(cn);
                    XtraMessageBox.Show("Xoá thành công!", "Xóa");
                    gvPhanCong.DeleteSelectedRows();
                    grcPhanCong.DataSource = cnbll.GetAllPhanCong();
                }
                catch
                {
                    XtraMessageBox.Show("Trùng khóa ngoại không thể xóa được!!!", "Xóa");
                }
            }
            this.Cursor = Cursors.Default;
        }
    }
}
